package com.yenlien.traceyun.plant.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.utils.EasyPoiUtils;
import com.yenlien.traceyun.plant.bean.DevDTO;
import com.yenlien.traceyun.plant.dao.AlarmDao;
import com.yenlien.traceyun.plant.dao.MassifDao;
import com.yenlien.traceyun.plant.dao.ShiJiRuiTongDao;
import com.yenlien.traceyun.plant.dock_device.sjrt.*;
import com.yenlien.traceyun.plant.entity.Massif;
import com.yenlien.traceyun.plant.entity.ShiJiRuiTong;
import com.yenlien.traceyun.plant.utils.MathUtils;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.yenlien.traceyun.plant.dock_device.DevState.EQU_TYPE_KILL_INSECT;
import static com.yenlien.traceyun.plant.dock_device.DevState.EQU_TYPE_VIDEO;

/**
 * @Date 2022/10/14
 * @Author jiaLong
 * @Descript 果园大屏
 */
@RestController
@RequestMapping("/fruitBigScreen")
public class FruitController extends BaseController {

    @Autowired
    private ShiJiRuiTongDao shiJiRuiTongDao;

    @Autowired
    private AlarmDao alarmDao;
    @Autowired
    private MassifDao massifDao;

    @Autowired
    private GetCWDevData devData;

    @Autowired
    private GetCWDevDataSF devDataSF;
    /*
    三丰使用萤石云的摄像头，  帐号：18030508642    密码： y123123   https://open.ys7.com/  一个10个摄像头
     */

//    List<DevDTO> allDev;
//    List<DevDTO> allDevCW;
//    List<DevDTO> allDevSF;

    public static List<DevDTO> sfVideoList = Arrays.asList(
            new DevDTO("1","1", "C53770424", "监控01", EQU_TYPE_VIDEO, "107.8761", "35.075108", "https://open.ys7.com/v3/openlive/C53770424_1_2.m3u8?expire=1709283774&id=553261304723275776&t=aa09909fd6f90a879746223febad335ccc1af8fb2abd6f0025075f462a1b519a&ev=100"),
            new DevDTO("1","2", "C53770424", "监控02", EQU_TYPE_VIDEO, "107.87598", "35.075914", "https://open.ys7.com/v3/openlive/C53770424_2_2.m3u8?expire=1709283796&id=553261398168174592&t=7d8c81a023e9ca14ac1c9af9a775fb7787034c6101a105f2580aea395f621b48&ev=100"),
            new DevDTO("1","3", "C53770424", "监控03", EQU_TYPE_VIDEO, "107.875231", "35.075913", "https://open.ys7.com/v3/openlive/C53770424_3_2.m3u8?expire=1709283815&id=553261477809623040&t=81acda83bac4e3a45200a98fb211b0153822923b3a4aab6c861b56cdb7383561&ev=100"),
            new DevDTO("1","4", "C53770424", "监控04", EQU_TYPE_VIDEO, "107.874721", "35.075808", "https://open.ys7.com/v3/openlive/C53770424_4_2.m3u8?expire=1709283873&id=553261720634654720&t=d8881768a7a354bd0f1b8ddadd8fc6c11f0973ff3de9dc3ee29ff794ab7ef608&ev=100"),
            new DevDTO("1","5", "C53770424", "监控05", EQU_TYPE_VIDEO, "107.874019", "35.075523", "https://open.ys7.com/v3/openlive/C53770424_5_2.m3u8?expire=1709283890&id=553261792189480961&t=39490aa97e2aee58d8ffd279dfc3f3024370dad8f0581be5f298205abe77a2fd&ev=100"),
            new DevDTO("1","6", "C53770424", "监控06", EQU_TYPE_VIDEO, "107.873304", "35.075277", "https://open.ys7.com/v3/openlive/C53770424_6_2.m3u8?expire=1709283910&id=553261877614870529&t=84371e5003768caaf937501d1d7d33a7e5650a3d063e53b28677e6bdb282bc50&ev=100"),
            new DevDTO("1","7", "C53770424", "监控07", EQU_TYPE_VIDEO, "107.874469", "35.076603", "https://open.ys7.com/v3/openlive/C53770424_7_2.m3u8?expire=1709284033&id=553262394084700160&t=b2482fd2dff3c895c8090498f17d8bc9e51b10b66aa1ea8e02d4d7c6a73f78d6&ev=100"),
            new DevDTO("1","8", "C53770424", "监控08", EQU_TYPE_VIDEO, "107.876754", "35.075901", "https://open.ys7.com/v3/openlive/C53770424_8_2.m3u8?expire=1709284065&id=553262524750192640&t=0c8d4fed983af5830196fe82707303da6801ddd9cceb033695a0ed8747d98901&ev=100"),
            new DevDTO("1","9", "C53770424", "监控09", EQU_TYPE_VIDEO, "107.877851", "35.07589", "https://open.ys7.com/v3/openlive/C53770424_9_2.m3u8?expire=1709284093&id=553262645424160768&t=04be7189d96e4d3c074d1a27b10e46e4b209e67e66824e4339fc3b2316167a42&ev=100"),
            new DevDTO("1","10", "C53770424", "监控10", EQU_TYPE_VIDEO, "107.878707", "35.075882", "https://open.ys7.com/v3/openlive/C53770424_10_2.m3u8?expire=1709284141&id=553262846067093504&t=fa5ea4534f6ae8e872bc38ab97591a6ac456dd96fe46e471ef47a3cc0ae06a59&ev=100"),

            new DevDTO("1","11", "C53770424", "监控11", EQU_TYPE_VIDEO, "107.8738612", "35.0738237", "https://open.ys7.com/v3/openlive/C53770424_10_2.m3u8?expire=1709284141&id=553262846067093504&t=fa5ea4534f6ae8e872bc38ab97591a6ac456dd96fe46e471ef47a3cc0ae06a59&ev=100"),
            new DevDTO("1","12", "C53770424", "监控12", EQU_TYPE_VIDEO, "107.876102", "35.0741366", "https://open.ys7.com/v3/openlive/C53770424_10_2.m3u8?expire=1709284141&id=553262846067093504&t=fa5ea4534f6ae8e872bc38ab97591a6ac456dd96fe46e471ef47a3cc0ae06a59&ev=100")
    );

    public static List<DevDTO> rjVideoList = Arrays.asList(
            new DevDTO("1","1", "L34072668", "监控1", EQU_TYPE_VIDEO, "107.74053", "35.15853", "https://open.ys7.com/v3/openlive/L34072668_1_2.m3u8?expire=1711677283&id=563300409569972224&t=f47117d387d2744fa390112b109f76cc9eb567ff4de468000df240cedb07a992&ev=100"),
            new DevDTO("1","4", "L34072668", "监控4", EQU_TYPE_VIDEO, "107.74025", "35.15725", "https://open.ys7.com/v3/openlive/L34072668_4_2.m3u8?expire=1711678151&id=563304053296074752&t=dc7dd270bdf360b7e04ceb559f3b79e1379f9ee854d9bdea62559fdd453b4e5e&ev=100"),
            new DevDTO("1","5", "L34072668", "监控5", EQU_TYPE_VIDEO, "107.73982", "35.15556", "https://open.ys7.com/v3/openlive/L34072668_5_2.m3u8?expire=1711678168&id=563304124087541760&t=fdc0714e8f385fe6767b0a65cd80d5d8bb2f647cdbb51169b73e15aeec405de9&ev=100"),
            new DevDTO("1","6", "L34072668", "监控6", EQU_TYPE_VIDEO, "107.73993", "35.15406", "https://open.ys7.com/v3/openlive/L34072668_6_2.m3u8?expire=1711678193&id=563304226290147328&t=65e213f7167681bfeaaca4ef30673bb4162aad5381330ff0d238c554d70d34d6&ev=100"),
            new DevDTO("1","7", "L34072668", "监控7", EQU_TYPE_VIDEO, "107.74038", "35.15304", "https://open.ys7.com/v3/openlive/L34072668_7_2.m3u8?expire=1711678250&id=563304468095967232&t=550050a26a9703c8b2f168371289f57dce20516ab28c196d404e0d4357b0c6ad&ev=100"),
            new DevDTO("1","9", "L34072668", "监控9", EQU_TYPE_VIDEO, "107.74181", "35.15742", "https://open.ys7.com/v3/openlive/L34072668_9_2.m3u8?expire=1711678286&id=563304618122018816&t=50581f31e97cad5883c725abbb5e6d043b05e867ac2b664631eac0537761fcd3&ev=100"),
            new DevDTO("1","10", "L34072668", "监控10", EQU_TYPE_VIDEO, "107.74342", "35.15639", "https://open.ys7.com/v3/openlive/L34072668_10_2.m3u8?expire=1711678337&id=563304830760660993&t=2a876be84ef7b7e4abd29dd3648542e823d047a875d3bf910ad8f0ca72fa82e6&ev=100"),
            new DevDTO("1","15", "L34072668", "监控15", EQU_TYPE_VIDEO, "107.74271", "35.155", "https://open.ys7.com/v3/openlive/L34072668_15_2.m3u8?expire=1711678487&id=563305462523498496&t=402fe539fac97ac80cf4d547a8b0ce2440f143731e1960ae2368ad691b58ebdf&ev=100"),
            new DevDTO("1","16", "L34072668", "监控16", EQU_TYPE_VIDEO, "107.74187", "35.15351", "https://open.ys7.com/v3/openlive/L34072668_16_2.m3u8?expire=1711678509&id=563305552520683520&t=c3d88359a0e8f31bfdda01857b81aa71758acc472de48ec9f72adb6dc76847c8&ev=100"),
            new DevDTO("1","17", "L34072668", "监控17", EQU_TYPE_VIDEO, "107.74265", "35.15228", "https://open.ys7.com/v3/openlive/L34072668_17_2.m3u8?expire=1711678545&id=563305702597066752&t=748857ae7a7b3125468dd8885b8709801bfc29aae0c3a6b3ca631af763492f8d&ev=100"),
            new DevDTO("1","24", "L34072668", "监控24", EQU_TYPE_VIDEO, "107.74473", "35.15634", "https://open.ys7.com/v3/openlive/L34072668_24_2.m3u8?expire=1714809139&id=576436366817107968&t=53f39615d3fa6feb7d606adcf90afa9af0c226afb9e9d9ce431172687d11a3a3&ev=100"),
            new DevDTO("1","27", "L34072668", "监控27", EQU_TYPE_VIDEO, "107.74522", "35.15544", "https://open.ys7.com/v3/openlive/L34072668_27_2.m3u8?expire=1714809204&id=576436639866294272&t=d9a947df874569b1f16518fb564e847104640717cf70981bd9bc5b1bc25578d3&ev=100"),
            new DevDTO("1","28", "L34072668", "监控28", EQU_TYPE_VIDEO, "107.74675", "35.15498", "https://open.ys7.com/v3/openlive/L34072668_28_2.m3u8?expire=1714809239&id=576436785739993088&t=ad21d6876d2564e596c5e91dd4d5d1ae56f8fe92404aa01b5731a6e76f35ae5b&ev=100"),


            new DevDTO("1","25", "L34072668", "监控25", EQU_TYPE_VIDEO, "107.74636", "35.15409", "https://open.ys7.com/v3/openlive/L34072668_25_2.m3u8?expire=1714809177&id=576436528167792640&t=0a2e2e19799bec4810b36811cd9e5c1639d424c9925c37afd8c682a5e549b821&ev=100"),
            new DevDTO("1","2", "L34072668", "监控2", EQU_TYPE_VIDEO, "107.74582", "35.15293", "https://open.ys7.com/v3/openlive/L34072668_2_2.m3u8?expire=1711678115&id=563303901923639296&t=0ec369025eb73c6a3506d4f0dbe78eb7c67466b783a1dc5d61deb1cfb4b190cd&ev=100"),
            new DevDTO("1","3", "L34072668", "监控3", EQU_TYPE_VIDEO, "107.74484", "35.15205", "https://open.ys7.com/v3/openlive/L34072668_3_2.m3u8?expire=1711678134&id=563303980436819968&t=26cd47bc3cc4cbc0642036f58d7336897f40363a5cb7d473d9635e89ea9cfd94&ev=100"),
            new DevDTO("1","8", "L34072668", "监控8", EQU_TYPE_VIDEO, "107.74919", "35.1544", "https://open.ys7.com/v3/openlive/L34072668_8_2.m3u8?expire=1711678270&id=563304548886646784&t=af366922c21601b6b820ab3fdcaaac1d45d59fe98c7bd1f8965b6c120466724b&ev=100"),
            new DevDTO("1","11", "L34072668", "监控11", EQU_TYPE_VIDEO, "107.74913", "35.15342", "https://open.ys7.com/v3/openlive/L34072668_11_2.m3u8?expire=1711678403&id=563305108350156800&t=d11c054e2f8962e689e8ac5172954a1ff99e25a44c072728b8e0aa4f10c308cc&ev=100"),
            new DevDTO("1","12", "L34072668", "监控12", EQU_TYPE_VIDEO, "107.74907", "35.15277", "https://open.ys7.com/v3/openlive/L34072668_12_2.m3u8?expire=1711678424&id=563305195967090688&t=f4fe3c963e7e80c76a89b14fb6ec2eee6e1c1ab90cae04bcd47cb3d85f77ea4e&ev=100"),
            new DevDTO("1","13", "L34072668", "监控13", EQU_TYPE_VIDEO, "107.7481", "35.15249", "https://open.ys7.com/v3/openlive/L34072668_13_2.m3u8?expire=1711678444&id=563305278284505088&t=bbcadb58ead543e13770f7ac5a11abdf96093e398b0f80e58e2b938c54f86f6b&ev=100"),
            new DevDTO("1","14", "L34072668", "监控14", EQU_TYPE_VIDEO, "107.74694", "35.15217", "https://open.ys7.com/v3/openlive/L34072668_14_2.m3u8?expire=1711678468&id=563305380646490112&t=df4b8147e49c02ccacd9baf74ca1ab24c74f7d48316e4f1f1ab004b8f4f5db94&ev=100")
//            new DevDTO("1","29", "L34072668", "监控22", EQU_TYPE_VIDEO, "107.8738612", "35.0738237", "https://open.ys7.com/v3/openlive/L34072668_28_2.m3u8?expire=1714809239&id=576436785739993088&t=ad21d6876d2564e596c5e91dd4d5d1ae56f8fe92404aa01b5731a6e76f35ae5b&ev=100"),
//            new DevDTO("1","18", "L34072668", "监控18", EQU_TYPE_VIDEO, "107.876102", "35.0741366", "https://open.ys7.com/v3/openlive/L34072668_18_2.m3u8?expire=1711678566&id=563305791944138752&t=661103eaa94ec2e06838ebe881194f2da0ced268b4c54255bb651c1c414cd419&ev=100")
//            new DevDTO("1","19", "L34072668", "监控19", EQU_TYPE_VIDEO, "107.7487359", "35.1526838", "https://open.ys7.com/v3/openlive/L34072668_19_2.m3u8?expire=1711678580&id=563305848569315328&t=af7e03dfe0dbae41effa7fc8501cdebb2956b50ed1ec0514fdb6188349d0c7de&ev=100"),
//            new DevDTO("1","20", "L34072668", "监控20", EQU_TYPE_VIDEO, "107.7482642", "35.1525417", "https://open.ys7.com/v3/openlive/L34072668_20_2.m3u8?expire=1711678604&id=563305950774046720&t=f8ab69b883ee8baa611ecd54e836f60ea1180217b47b44ff4211e6cf907d569b&ev=100"),
//            new DevDTO("1","21", "L34072668", "监控21", EQU_TYPE_VIDEO, "107.7469146", "35.1521241", "https://open.ys7.com/v3/openlive/L34072668_21_2.m3u8?expire=1711678625&id=563306040104325120&t=71a50c678de6f490dab4377e33472c4cef410f48a3bf7e068f3f8cc5497a930c&ev=100")
    );


    public static  List<String> idList = Arrays.asList("866250060552647", "866250060560277", "866250060599531", "866250060601519", "866250060585191", "866250061178004",
            "866547058633994", "D83590999-D2", "D83590999-D77", "D83590999-D13");




    // 加载首页数据
    @GetMapping(value = "/getDevData")
    public Object getDevData(String type) {
        List<DevDTO> allDev;
        if (StringUtils.isBlank(type))
            type = "cw";
        if ("cw".equals(type)){
            GetCWDevData.getToken();
            allDev = GetCWDevData.getAllDev();
//            if(CollectionUtils.isNotEmpty(GetCWDevData.allDev)){
//                allDev = GetCWDevData.allDev;
//            }else{
//                allDev = GetCWDevData.getAllDev();
//                GetCWDevData.allDev = allDev;
//            }
        }
        else if ("sf".equals(type)){
            GetCWDevDataSF.getToken();
            allDev = GetCWDevDataSF.getAllDev();
//            if(CollectionUtils.isNotEmpty(GetCWDevDataSF.allDev)){
//                allDev = GetCWDevDataSF.allDev;
//            }else{
//                allDev = GetCWDevDataSF.getAllDev();
//                GetCWDevDataSF.allDev = allDev;
//            }
        }
        else if("qfx".equals(type)){
            GetCWDevData.getToken();
            allDev = GetCWDevData.getAllDev();
//            if(CollectionUtils.isNotEmpty(GetCWDevData.allDev)){
//                allDev = GetCWDevData.allDev;
//            }else{
//                allDev = GetCWDevData.getAllDev();
//                GetCWDevData.allDev = allDev;
//            }
            allDev = allDev.stream().filter(x -> idList.contains(x.getDevId())).collect(Collectors.toList());
        }
        else if("rj".equals(type)){
            GetCWDevDataRJ.getToken();
            allDev = GetCWDevDataRJ.getAllDev();
//            if(CollectionUtils.isNotEmpty(GetCWDevDataRJ.allDev)){
//                allDev = GetCWDevDataRJ.allDev;
//            }else{
//                allDev = GetCWDevDataRJ.getAllDev();
//                GetCWDevDataRJ.allDev = allDev;
//            }
        }
        else
            return fail("果园信息不正确");


//        Map<String, List<DevDTO>> devMap = allDev.stream().collect(Collectors.groupingBy(DevDTO::getDevType));
        // 统计设备在线离线数量并获取气象传感数据
        Integer onlineNum = 0;
        Integer offlineNum = 0;
        JSONObject object = new JSONObject();
        LocalDateTime now = LocalDateTime.now();

        for (DevDTO devDTO : allDev) {
            Duration dur = null;
            try {
                dur = Duration.between(devDTO.getHeartbeatTime(), now);
            }catch (Exception e){

            }
            if(dur == null){
                devDTO.setState("2");
                continue;
            }

            if ("虫情测报仪".equals(devDTO.getDevType()) || EQU_TYPE_KILL_INSECT.equals(devDTO.getDevType()) || EQU_TYPE_VIDEO.equals(devDTO.getDevType())) {
                if (dur.toMinutes() > 60) {
                    offlineNum += 1;
                    devDTO.setState("2");
                } else {
                    onlineNum += 1;
                    devDTO.setState("1");
                }
            } else {
                if (dur.toMinutes() > 10) {
                    offlineNum += 1;
                    devDTO.setState("2");
                } else {
                    onlineNum += 1;
                    devDTO.setState("1");
                }
            }

            if("qfx".equals(type)){
                if("866250060552647".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7208063");
                    devDTO.setLat("35.1251990");
                }
                if("866250060560277".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7212526");
                    devDTO.setLat("35.1232193");
                }
                if("866250060599531".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7202398");
                    devDTO.setLat("35.1274594");
                }
                if("866250060601519".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7233125");
                    devDTO.setLat("35.1260695");
                }
                if("866250060585191".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7232374");
                    devDTO.setLat("35.1235709");
                }
                if("866250061178004".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7222310");
                    devDTO.setLat("35.1255781");
                }
                if("866547058633994".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7245816");
                    devDTO.setLat("35.1268165");
                }
                if("D83590999-D2".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7211324");
                    devDTO.setLat("35.1289195");
                }
                if("D83590999-D77".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7234670");
                    devDTO.setLat("35.1255921");
                }
                if("D83590999-D13".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7193986");
                    devDTO.setLat("35.1251428");
                }
            }
            else if("sf".equals(type)){
                if("866250060578113".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8741125");
                    devDTO.setLat("35.0761110");
                }
                if("866250060589003".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8749923");
                    devDTO.setLat("35.0763463");
                }
                if("866250060597584".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8773440");
                    devDTO.setLat("35.0751908");
                }
                if("866250060602723".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8760094");
                    devDTO.setLat("35.0734944");
                }
                if("866250060590894".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8741812");
                    devDTO.setLat("35.0755998");
                }
                if("866250060544214".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8766593");
                    devDTO.setLat("35.0758447");
                }
                if("866250060602723".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8772675");
                    devDTO.setLat("35.0758497");
                }
                if("866250060589003".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8744559");
                    devDTO.setLat("35.075646");
                }
                if("866250060578113".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8758741");
                    devDTO.setLat("35.0759453");
                }
                if("866250060597584".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7402395");
                    devDTO.setLat("35.1573188");
                }
                if("866250060503087".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7398617");
                    devDTO.setLat("35.1556297");
                }
                if("866250061177683".equals(devDTO.getDevId())){
                    devDTO.setLng("107.875137");
                    devDTO.setLat("35.0758431");
                }
                if("866250061176222".equals(devDTO.getDevId())){
                    devDTO.setLng("107.8761183");
                    devDTO.setLat("35.0751071");
                }
            }
            else if("cw".equals(type)){
                if("866250060601519".equals(devDTO.getDevId())){
                    devDTO.setLng("107.9070227");
                    devDTO.setLat("35.0734470");
                }
                if("866250060599531".equals(devDTO.getDevId())){
                    devDTO.setLng("107.9092157");
                    devDTO.setLat("35.0735594");
                }
                if("866250060597931".equals(devDTO.getDevId())){
                    devDTO.setLng("107.9069969");
                    devDTO.setLat("35.0731800");
                }
                if("866250060596255".equals(devDTO.getDevId())){
                    devDTO.setLng("107.9077222");
                    devDTO.setLat("35.0731800");
                }
                if("866250060584855".equals(devDTO.getDevId())){
                    devDTO.setLng("107.9093916");
                    devDTO.setLat("35.0729728");
                }
                if("866250060556655".equals(devDTO.getDevId())){
                    devDTO.setLng("107.9086535");
                    devDTO.setLat("35.0705001");
                }
                if("866250060552647".equals(devDTO.getDevId())){
                    devDTO.setLng("107.9080827");
                    devDTO.setLat("35.0705633");
                }
            }
            else if("rj".equals(type)){
                // 杀虫灯
                if("865328061479555".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74185");
                    devDTO.setLat("35.15686");
                }
                if("865328061480850".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74107");
                    devDTO.setLat("35.15409");
                }
                if("865328061476833".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74457");
                    devDTO.setLat("35.15542");
                }
                if("865328061481106".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74414");
                    devDTO.setLat("35.15302");
                }
                if("865328061442363".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74839");
                    devDTO.setLat("35.15492");
                }
                if("865328061272794".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74719");
                    devDTO.setLat("35.15281");
                }

                // 虫情
                if("866547058636930".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74575");
                    devDTO.setLat("35.15335");
                }

                // 土壤
                if("866250060554569".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7409");
                    devDTO.setLat("35.15736");
                }if("866250060599119".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7410");
                    devDTO.setLat("35.15547");
                }if("866250060590233".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74047");
                    devDTO.setLat("35.15385");
                }if("866250060557455".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74143");
                    devDTO.setLat("35.15271");

                }if("866250060605650".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74266");
                    devDTO.setLat("35.15582");

                }if("866250060557331".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74419");
                    devDTO.setLat("35.15567");
                }
                if("866250060588419".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74382");
                    devDTO.setLat("35.15465");
                }
                if("866250060601576".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74324");
                    devDTO.setLat("35.15325");
                }
                if("866250060503061".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74599");
                    devDTO.setLat("35.15483");
                }
                if("866250060567512".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74537");
                    devDTO.setLat("35.15407");
                }
                if("866250060598376".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74421");
                    devDTO.setLat("35.15248");
                }
                if("866250060590498".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74764");
                    devDTO.setLat("35.15427");
                }
                if("866250060560210".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74732");
                    devDTO.setLat("35.15285");
                }
                if("866250060573536".equals(devDTO.getDevId())){
                    devDTO.setLng("107.74219");
                    devDTO.setLat("35.15474");
                }


                // 气象
                if("866250060601592".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7419406");
                    devDTO.setLat("35.1561373");
                }
                if("866250060597881".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7445936");
                    devDTO.setLat("35.1545441");
                }
                if("866250060599283".equals(devDTO.getDevId())){
                    devDTO.setLng("107.7474249");
                    devDTO.setLat("35.1533482");
                }

//                if("866250060554569".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7466452");
//                    devDTO.setLat("35.1546227");
//                }
//                if("866250060599119".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7475127");
//                    devDTO.setLat("35.1523415");
//                }
//                if("866250060590233".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7459534");
//                    devDTO.setLat("35.1540235");
//                }
//                if("866250060557455".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7458520");
//                    devDTO.setLat("35.1527510");
//                }
//                if("866250060605650".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7463105");
//                    devDTO.setLat("35.1545759");
//                }
//                if("866250060557331".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7433242");
//                    devDTO.setLat("35.1560642");
//                }
//                if("866250060588419".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7425034");
//                    devDTO.setLat("35.1542342");
//                }
//                if("866250060601576".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7430755");
//                    devDTO.setLat("35.1553663");
//                }
//                if("866250060503061".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7411110");
//                    devDTO.setLat("35.1555925");
//                }
//                if("866250060567512".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7412071");
//                    devDTO.setLat("35.1559664");
//                }
//                if("866250060598376".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7414878");
//                    devDTO.setLat("35.1567163");
//                }
//                if("866250060590498".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7416234");
//                    devDTO.setLat("35.1569974");
//                }
//                if("866250060560210".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7417309");
//                    devDTO.setLat("35.1573472");
//                }
//                if("866250060573536".equals(devDTO.getDevId())){
//                    devDTO.setLng("107.7418926");
//                    devDTO.setLat("35.1577995");
//                }
            }
            if (StringUtils.isNotBlank(object.getString("envData")))
                continue;
            // 获取一个当前气象数据
            if ("气象监测站".equals(devDTO.getDevType())) {
//                if ("cw".equals(type))
//                    object.put("envData", devData.getNowData(devDTO.getDevId()));
//                else if ("sf".equals(type))
//                    object.put("envData", devDataSF.getNowData(devDTO.getDevId()));
//                else if ("qfx".equals(type))
//                    object.put("envData", devData.getNowData(devDTO.getDevId()));
//                else if ("rj".equals(type))
//                    object.put("envData", GetCWDevDataRJ.getNowData(devDTO.getDevId()));
                // 不取及时数据
                object.put("envData", shiJiRuiTongDao.getLast(devDTO.getDevId()));
            }
        }

        List<ShiJiRuiTong> dataList = new ArrayList<>();
//        if ("cw".equals(type))
//            dataList = devData.getNowData(allDev.stream().filter(x -> "土壤墒情站".equals(x.getDevType())).findAny().get().getDevId());
//        else if ("sf".equals(type))
//            dataList = devDataSF.getNowData(allDev.stream().filter(x -> "土壤墒情站".equals(x.getDevType())).findAny().get().getDevId());
//        else if ("qfx".equals(type))
//            dataList = devData.getNowData(allDev.stream().filter(x -> "土壤墒情站".equals(x.getDevType())).findAny().get().getDevId());
//        else if ("rj".equals(type))
//            dataList = GetCWDevDataRJ.getNowData(allDev.stream().filter(x -> "土壤墒情站".equals(x.getDevType())).findAny().get().getDevId());

        dataList = JSONArray.parseArray(shiJiRuiTongDao.getLast(allDev.stream().filter(x -> "土壤墒情站".equals(x.getDevType())).findAny().get().getDevId()).toString(), ShiJiRuiTong.class);
        object.put("soilData", dataList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ShiJiRuiTong::getParamName))), ArrayList::new)));

        List<ShiJiRuiTong> cwInsect = new ArrayList<>();
        if ("cw".equals(type) || "qfx".equals(type))
            cwInsect = shiJiRuiTongDao.getCWInsect("cw");
        else
            cwInsect = shiJiRuiTongDao.getCWInsect(type);
        Map<String, List<ShiJiRuiTong>> listMap = cwInsect.stream().collect(Collectors.groupingBy(x -> x.getCtime().substring(0, 10)));
        List<JSONObject> list = new ArrayList<>();
        for (String key : listMap.keySet()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("date", key);
            jsonObject.put("value", listMap.get(key).size());
            list.add(jsonObject);
        }
        if("sf".equals(type))
            onlineNum += sfVideoList.size();
        if("rj".equals(type))
            onlineNum += rjVideoList.size();
        Integer total = onlineNum + offlineNum;
        object.put("insect", list);                 // 虫情图表
        object.put("insectData", cwInsect.stream().limit(8).collect(Collectors.toList()));         // 虫情数据
        object.put("onlineNum", onlineNum);         // 在线数量
        object.put("onlineNumRatio", MathUtils.div(onlineNum, total) + "%");    // 在线百分比
        object.put("offlineNum", offlineNum);       // 离线数量
        object.put("offlineNumRatio", MathUtils.div(offlineNum, total) + "%");  // 离线百分比

        List<DevDTO> videoList;
        if("sf".equals(type)){
            videoList = sfVideoList;
            object.put("videoList", sfVideoList);
        }else if("rj".equals(type)){
            videoList = rjVideoList;
            object.put("videoList", rjVideoList);
        } else {
            videoList = allDev.stream().filter(x -> EQU_TYPE_VIDEO.equals(x.getDevType())).collect(Collectors.toList());
            object.put("videoList", videoList);
        }
        JSONObject videoDataJson = new JSONObject();
        if(CollectionUtils.isEmpty(videoList)){
            object.put("videoData", "");
        }else {
            if ("cw".equals(type) || "qfx".equals(type)) {
                object.put("videoData", devData.getVideo(videoList.get(0).getDevId()));   // 摄像机播放地址
                object.put("alarmList", alarmDao.getHistoryDataNum(10, "cw"));                // 报警数据
            } else if ("sf".equals(type)) {
//                object.put("videoData", devDataSF.getVideo(videoList.get(0).getDevId()));     三丰使用萤石云摄像头
                videoDataJson.put("OnlineAddr", sfVideoList.get(0).getUrl());
                videoDataJson.put("DevName", sfVideoList.get(0).getDevName());
                videoDataJson.put("DevId", sfVideoList.get(0).getDevId());
                videoDataJson.put("channel", sfVideoList.get(0).getChannel());
                object.put("videoData", videoDataJson);
                object.put("alarmList", alarmDao.getHistoryDataNum(10, "sf"));
            }else if("rj".equals(type)){
                videoDataJson.put("OnlineAddr", rjVideoList.get(0).getUrl());
                videoDataJson.put("DevName", rjVideoList.get(0).getDevName());
                videoDataJson.put("DevId", rjVideoList.get(0).getDevId());
                videoDataJson.put("channel", rjVideoList.get(0).getChannel());
                object.put("videoData", videoDataJson);
                object.put("alarmList", alarmDao.getHistoryDataNum(10, "rj"));
            }
        }
        object.put("envList", allDev.stream().filter(x -> "气象监测站".equals(x.getDevType())).collect(Collectors.toList()));
        object.put("soilList", allDev.stream().filter(x -> "土壤墒情站".equals(x.getDevType())).collect(Collectors.toList()));
        object.put("killInsectList", allDev.stream().filter(x -> EQU_TYPE_KILL_INSECT.equals(x.getDevType())).collect(Collectors.toList()));
        object.put("insectList", allDev.stream().filter(x -> "虫情测报仪".equals(x.getDevType())).collect(Collectors.toList()));
        return success(object);
    }


    // 获取播放地址
    @GetMapping(value = "/getVideoUrl")
    private Object getVideoUrl(String devId, String type) {
        if (StringUtils.isBlank(type))
            type = "cw";
        if ("sf".equals(type)) {
            GetCWDevDataSF.getToken();
            return success(GetCWDevDataSF.getVideo(devId));
        }else if("cw".equals(type)){
            GetCWDevData.getToken();
            return success(GetCWDevData.getVideo(devId));
        }else if("qfx".equals(type)){
            GetCWDevData.getToken();
            return success(GetCWDevData.getVideo(devId));
        }
        return fail("果园信息不正确");
    }


    LocalDate date;
    String YSYToken;

    // 萤石云旋转接口
//    @GetMapping(value = "/videoMove")
//    private Object videoMove(String devId, String channel) {
//        if(StringUtils.isBlank(YSYToken)){
//            JSONObject tokenParam = new JSONObject();
//            tokenParam.put("appKey", "6f4f1f1c3ed04f58a4f140b5a997ef76");
//            tokenParam.put("appSecret", "cb5f4c3fdadd4804ec4418de783d0381");
//            String result = HttpUtil.post("https://open.ys7.com/api/lapp/token/get", tokenParam);
//        }else {
//            if()
//
//        }
//        return fail("果园信息不正确");
//    }


    // 萤石云token接口
    @GetMapping(value = "/getToken")
    private Object getToken() {
        JSONObject param = new JSONObject();
        param.put("appKey", "6f4f1f1c3ed04f58a4f140b5a997ef76");
        param.put("appSecret", "cb5f4c3fdadd4804ec4418de783d0381");
        String post = HttpUtil.post("https://open.ys7.com/api/lapp/token/get", param);
        String token = "";
        JSONObject result = JSONObject.parseObject(post);
        if("200".equals(result.getString("code")))
             token = result.getJSONObject("data").getString("accessToken");
        else
            throw new BusinessException("请求token失败");
        return success(token);
    }

    // 控制视频旋转
    @GetMapping(value = "/videoMove")
    private Object videoMove(String devId, String val, String type) {
        if (StringUtils.isBlank(type))
            type = "cw";
        if ("sf".equals(type)) {
//            JSONObject param = new JSONObject();
//            param.put("appKey", "6f4f1f1c3ed04f58a4f140b5a997ef76");
//            param.put("appSecret", "cb5f4c3fdadd4804ec4418de783d0381");
//            String post = HttpUtil.post("https://open.ys7.com/api/lapp/token/get", param);
//            if(){
//
//            }

            GetCWDevDataSF.getToken();
            return success(GetCWDevDataSF.videoMove(devId, val));
        }else if("cw".equals(type)){
            GetCWDevData.getToken();
            return success(GetCWDevData.videoMove(devId, val));
        }else if("qfx".equals(type)){
            GetCWDevData.getToken();
            return success(GetCWDevData.videoMove(devId, val));
        }
        return fail("果园信息不正确");
    }


    @GetMapping(value = "/getNowData")
    public Object getNowData(String devId, String devType, String type) {
        // devType 气象数据  土壤数据  虫情监控 水质 杀虫灯 摄像机
        List<DevDTO> allDev = new ArrayList<>();
        if (StringUtils.isBlank(type))
            type = "cw";
        if ("cw".equals(type))
            GetCWDevData.getToken();
        else if ("sf".equals(type))
            GetCWDevDataSF.getToken();
        else if ("qfx".equals(type))
            GetCWDevData.getToken();
        else if ("rj".equals(type))
            GetCWDevDataRJ.getToken();
        else
            return fail("果园信息不正确");

//        if ("cw".equals(type)) {
//            if (CollectionUtils.isEmpty(GetCWDevData.allDev)) {
//                allDev = GetCWDevData.getAllDev();
//                GetCWDevData.allDev = allDev;
//            } else{
//                allDev = GetCWDevData.getAllDev();
//                GetCWDevData.allDev = allDev;
//            }
//        } else if ("sf".equals(type)){
//            if (CollectionUtils.isEmpty(GetCWDevDataSF.allDev)) {
//                allDev = GetCWDevDataSF.getAllDev();
//                GetCWDevDataSF.allDev = allDev;
//            } else{
//                allDev = GetCWDevDataSF.getAllDev();
//                GetCWDevDataSF.allDev = allDev;
//            }
//        }else if("qfx".equals(type)){
//            if (CollectionUtils.isEmpty(GetCWDevData.allDev)) {
//                allDev = GetCWDevData.getAllDev();
//                GetCWDevData.allDev = allDev;
//            } else{
//                allDev = GetCWDevData.getAllDev();
//                GetCWDevData.allDev = allDev;
//            }
//        }else if("rj".equals(type)){
//            if (CollectionUtils.isEmpty(GetCWDevDataRJ.allDev)) {
//                allDev = GetCWDevDataRJ.getAllDev();
//                GetCWDevDataRJ.allDev = allDev;
//            } else{
//                allDev = GetCWDevDataRJ.getAllDev();
//                GetCWDevDataRJ.allDev = allDev;
//            }
//        }


        if ("cw".equals(type)){
//            if(CollectionUtils.isNotEmpty(GetCWDevData.allDev)){
//                allDev = GetCWDevData.allDev;
//            }else{
                allDev = GetCWDevData.getAllDev();
                GetCWDevData.allDev = allDev;
//            }
        }
        else if ("sf".equals(type)){
//            if(CollectionUtils.isNotEmpty(GetCWDevDataSF.allDev)){
//                allDev = GetCWDevDataSF.allDev;
//            }else{
                allDev = GetCWDevDataSF.getAllDev();
                GetCWDevDataSF.allDev = allDev;
//            }
        }
        else if("qfx".equals(type)){
//            if(CollectionUtils.isNotEmpty(GetCWDevData.allDev)){
//                allDev = GetCWDevData.allDev;
//            }else{
                allDev = GetCWDevData.getAllDev();
                GetCWDevData.allDev = allDev;
//            }
            allDev = allDev.stream().filter(x -> idList.contains(x.getDevId())).collect(Collectors.toList());
        }
        else if("rj".equals(type)){
//            if(CollectionUtils.isNotEmpty(GetCWDevDataRJ.allDev)){
//                allDev = GetCWDevDataRJ.allDev;
//            }else{
                allDev = GetCWDevDataRJ.getAllDev();
                GetCWDevDataRJ.allDev = allDev;
//            }
        }

        LocalDateTime now = LocalDateTime.now();
        for (DevDTO devDTO : allDev) {
            Duration dur = null;
            try {
                dur = Duration.between(devDTO.getHeartbeatTime(), now);
            }catch (Exception e){
                devDTO.setState("2");
            }
            if(dur == null){
                devDTO.setState("2");
                continue;
            }

            if ("虫情测报仪".equals(devDTO.getDevType()) || EQU_TYPE_KILL_INSECT.equals(devDTO.getDevType()) || EQU_TYPE_VIDEO.equals(devDTO.getDevType())) {
                if (dur.toMinutes() > 60) {
                    devDTO.setState("2");
                } else {
                    devDTO.setState("1");
                }
            } else {
                if (dur.toMinutes() > 10) {
                    devDTO.setState("2");
                } else {
                    devDTO.setState("1");
                }
            }
        }


        JSONObject jsonObject = new JSONObject();
        if ("气象监测站".equals(devType) || "土壤墒情站".equals(devType) || EQU_TYPE_KILL_INSECT.equals(devType)) {
//            if ("cw".equals(type))
//                jsonObject.put("data", devData.getNowData(devId));
//            else if ("sf".equals(type))
//                jsonObject.put("data", devDataSF.getNowData(devId));
//            else if("qfx".equals(type))
//                jsonObject.put("data", devData.getNowData(devId));
//            else if("rj".equals(type)){
//                jsonObject.put("data", GetCWDevDataRJ.getNowData(devId));
//            }
            jsonObject.put("data", shiJiRuiTongDao.getLast(devId));
            jsonObject.put("devList", allDev.stream().filter(x -> devType.equals(x.getDevType())).collect(Collectors.toList()));
            return success(jsonObject);
        } else if ("虫情测报仪".equals(devType)) {
            List<ShiJiRuiTong> insectHistoryData = new ArrayList<>();
//            if ("cw".equals(type)) {
//                insectHistoryData = devData.getInsectHistoryData(devId, LocalDate.now().minusDays(1).toString(), LocalDate.now().toString(), GetCWDevData.getInsectHistoryDataUrl)
//                        .stream().sorted(Comparator.comparing(ShiJiRuiTong::getVal).reversed()).limit(10).collect(Collectors.toList());
//            } else if ("sf".equals(type)) {
//                insectHistoryData = devDataSF.getInsectHistoryData(devId, LocalDate.now().minusDays(1).toString(), LocalDate.now().toString(), GetCWDevData.getInsectHistoryDataUrl)
//                        .stream().sorted(Comparator.comparing(ShiJiRuiTong::getVal).reversed()).limit(10).collect(Collectors.toList());
//            }else if("qfx".equals(type))
//                insectHistoryData = devData.getInsectHistoryData(devId, LocalDate.now().minusDays(1).toString(), LocalDate.now().toString(), GetCWDevData.getInsectHistoryDataUrl)
//                        .stream().sorted(Comparator.comparing(ShiJiRuiTong::getVal).reversed()).limit(10).collect(Collectors.toList());
//            else if("rj".equals(type)){
//                insectHistoryData = GetCWDevDataRJ.getInsectHistoryData(devId, LocalDate.now().minusDays(1).toString(), LocalDate.now().toString(), GetCWDevData.getInsectHistoryDataUrl)
//                        .stream().sorted(Comparator.comparing(ShiJiRuiTong::getVal).reversed()).limit(10).collect(Collectors.toList());
//            }
            insectHistoryData = shiJiRuiTongDao.getInsectHistoryData(devId);
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            for (ShiJiRuiTong shiJiRuiTong : insectHistoryData) {
//                String time = LocalDateTime.parse(shiJiRuiTong.getVal().split("\\.")[0].replace("T", " "), formatter).format(formatter);
//                shiJiRuiTong.setVal(time);
//            }
            jsonObject.put("data", insectHistoryData);
            jsonObject.put("devList", allDev.stream().filter(x -> devType.equals(x.getDevType())).collect(Collectors.toList()));
            return success(insectHistoryData);
        } else if (EQU_TYPE_VIDEO.equals(devType)) {
            // 请求摄像头播放地址
            if ("cw".equals(type))
                jsonObject.put("data", GetCWDevData.getVideo(devId));
            else if ("sf".equals(type))
                jsonObject.put("data", GetCWDevDataSF.getVideo(devId));
            else if ("rj".equals(type))
                jsonObject.put("data", GetCWDevDataRJ.getVideo(devId));
            else if("qfx".equals(type))
                jsonObject.put("data", GetCWDevData.getVideo(devId));
            jsonObject.put("devList", allDev.stream().filter(x -> devType.equals(x.getDevType())).collect(Collectors.toList()));
        }
        return fail("参数错误");
    }







    @GetMapping(value = "/getHistoryData")
    public Object getHistoryData(String devId, String devType, String startTime, String endTime, String type, Integer page, Integer pageSize) {
        if (StringUtils.isBlank(type))
            type = "cw";

        if(EQU_TYPE_KILL_INSECT.equals(devType)){
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime))
                return success(shiJiRuiTongDao.getHistoryData(devId, startTime, endTime));
            else
                return success(shiJiRuiTongDao.getHistoryData(devId));
        }
        if ("气象监测站".equals(devType)) {
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                return success(shiJiRuiTongDao.getHistoryData(devId, startTime, endTime).stream().collect(Collectors.groupingBy(ShiJiRuiTong::getParamName)));
            } else {
                return success(shiJiRuiTongDao.getHistoryData(devId).stream().collect(Collectors.groupingBy(ShiJiRuiTong::getParamName)));
            }
        }if("土壤墒情站".equals(devType)){
            List<ShiJiRuiTong> soilHistoryList;
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime))
                soilHistoryList = shiJiRuiTongDao.getHistoryData(devId, startTime, endTime);
            else
                soilHistoryList = shiJiRuiTongDao.getHistoryData(devId);
            Map<String, List<ShiJiRuiTong>> mapList = soilHistoryList.stream().filter(x -> x.getParamName().contains("土壤温度") || x.getParamName().contains("土壤湿度")).collect(Collectors.groupingBy(ShiJiRuiTong::getParamName));
            List<JSONObject> list = new ArrayList<>();
            for(String key : mapList.keySet()){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("paramName", key);
                jsonObject.put("dataList", mapList.get(key));
                list.add(jsonObject);
            }
            Map<String, List<ShiJiRuiTong>> mapList2 = soilHistoryList.stream().filter(x -> !x.getParamName().contains("土壤温度") && !x.getParamName().contains("土壤湿度")).collect(Collectors.groupingBy(ShiJiRuiTong::getParamName));
            for(String key : mapList2.keySet()){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("paramName", key);
                jsonObject.put("dataList", mapList2.get(key));
                list.add(jsonObject);
            }
            return list;
        } else if ("虫情测报仪".equals(devType)) {
            Pageable pageable = PageRequest.of(page - 1, pageSize);
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                return success(shiJiRuiTongDao.getHistoryData(devId, startTime, endTime, pageable));
            } else {
                return success(shiJiRuiTongDao.getHistoryData(devId, pageable));
            }
        }
        return fail("参数错误");
    }


    // 数据导出
    @GetMapping(value = "/export")
    public void export(String devId, String startTime, String endTime, HttpServletResponse response) {
        List<ShiJiRuiTong> dataList;
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            dataList = shiJiRuiTongDao.getHistoryData(devId, startTime, endTime);
        } else {
            dataList = shiJiRuiTongDao.getHistoryData(devId);
        }
        try {
            EasyPoiUtils.exportExcel(dataList, "设备数据", "设备数据", ShiJiRuiTong.class, "设备数据", response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @PostMapping(value = "/saveMassif")
    public Object getHistoryData(@RequestBody Massif massif) {
        return success(massifDao.save(massif));
    }

    @GetMapping(value = "/findAllMassif")
    public Object findAllMassif() {
        return success(massifDao.findAll());
    }





}
